package com.cxs.tetrisdemo.tetris;

import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Random;

import static com.cxs.tetrisdemo.common.Constant.*;


public class Tetris extends TetrisBlock {
    Context context;
    private Position curPos;
    private Position targetPos;
    private int centerX = 3;
    private int fastNum = 0;

    public Tetris(Context context, int len) {
        super(context, len);
        this.context = context;
        // 初始化游戏场景
        initGrid();
    }


    void initGrid() {
        curPos = new Position(centerX, -1);
        targetPos = new Position(centerX, -1);
        block = generateNextBlock();
        // 画网格
        addDrawTask((component, canvas) -> {
            drawGrid(canvas);

        });
    }

    public void startGame() {

        eventHandler.sendEvent(START_GAME, SPEED);
    }

    public void pauseGame() {
        eventHandler.removeEvent(START_GAME);
    }

    public void stopGame() {
        eventHandler.sendEvent(STOP_GAME);
    }

    public void moveTetrisBlock(int move) {
        switch (move) {
            case MOVE_LEFT: {
                eventHandler.removeEvent(START_GAME);
                targetPos.X--;
                eventHandler.sendEvent(START_GAME, 0);
                break;
            }
            case MOVE_RIGHT: {
                eventHandler.removeEvent(START_GAME);
                targetPos.X++;
                eventHandler.sendEvent(START_GAME, 0);
                break;
            }
            case MOVE_DOWN: {
                eventHandler.removeEvent(START_GAME);
                eventHandler.sendEvent(START_GAME, 0);
                break;
            }
        }
    }
    /*
        方块旋转
     */
    public void rotateTetrisBlock() {
        eventHandler.removeEvent(START_GAME);
        block.rorate();
        eventHandler.sendEvent(START_GAME, 0);
    }

    EventHandler eventHandler = new EventHandler(EventRunner.create()) {
        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            switch (event.eventId) {
                case START_GAME: {
                    // 方块的目标位置向下移动一行
                    if (curPos.X == targetPos.X) {
                        targetPos.Y++;
                    }
                    // 如果受阻，判断是哪个方向
                    if (setGrids(targetPos, block)) {
                        // X轴不变，则是向下移动受阻
                        if (curPos.X == targetPos.X) {
                            // 消行
                            checkEliminate();
                            // grids的值赋给preGrids
                            copyGrids(grids, preGrids);
                            targetPos.X = centerX;
                            targetPos.Y = -1;
                            // 下一个方块下落
                            block = generateNextBlock();
                            sendEvent(START_GAME, 0);
                        } else {
                            // X轴改变，是左右移动受阻，继续循环
                            targetPos.X = curPos.X;
                            sendEvent(START_GAME, 1000);
                        }
                    } else {
                        // 如果没有受阻,更新当前网格
                        context.getUITaskDispatcher().delayDispatch(() -> {
                            addDrawTask((c, canvas) -> {
                                drawTetrisGrids(canvas);
                            });
                        }, 0);
                        curPos.X = targetPos.X;
                        curPos.Y = targetPos.Y;
                        sendEvent(START_GAME, 1000);
                    }

                    break;
                }
                case PAUSE_GAME: {

                }
                case STOP_GAME: {

                }
            }
        }
    };

    /*
        生成下一个方块
     */
    BlockStyle generateNextBlock() {
        nextBlock = new BlockStyle(new Random().nextInt(7), new Random().nextInt(4));
        return nextBlock;
    }

}
